package jnpf.engine.custom.cmd.jump;

import org.flowable.bpmn.model.Activity;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.MultiInstanceLoopCharacteristics;
import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.impl.bpmn.behavior.MultiInstanceActivityBehavior;
import org.flowable.engine.impl.bpmn.behavior.ParallelMultiInstanceBehavior;
import org.flowable.engine.impl.history.HistoryManager;
import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityManager;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.engine.impl.util.ProcessDefinitionUtil;

import java.io.Serializable;
import java.util.Map;

/**
 * 会签加签CMD
 */
public class AddMulitiInstanceExecutionCmd implements Command<Void>, Serializable {
	protected final String NUMBER_OF_INSTANCES = "nrOfInstances";
	protected final String NUMBER_OF_ACTIVE_INSTANCES = "nrOfActiveInstances";
	protected final String NUMBER_OF_COMPLETED_INSTANCES = "nrOfCompletedInstances";
	protected String collectionElementIndexVariable = "loopCounter";
	private String parentExecutionId;
	private String activityId;
	private Map<String, Object> executionVariables;

	/**
	 *
	 * @param parentExecutionId  父执行实例id
	 * @param activityId 目标节点id
	 * @param executionVariables 变量
	 */
	public AddMulitiInstanceExecutionCmd(String parentExecutionId, String activityId,
                                         Map<String, Object> executionVariables) {
		this.parentExecutionId = parentExecutionId;
		this.activityId = activityId;
		this.executionVariables = executionVariables;
	}

	@Override
	public Void execute(CommandContext commandContext) {
		return null;
	}

	protected Integer getLoopVariable(DelegateExecution execution, String variableName) {
		Object value = execution.getVariableLocal(variableName);
		DelegateExecution parent = execution.getParent();
		while (value == null && parent != null) {
			value = parent.getVariableLocal(variableName);
			parent = parent.getParent();
		}
		return (Integer) (value != null ? value : 0);
	}
}
